<?php

namespace app\admin\controller\machine;

use app\admin\traits\Curd;
use app\common\business\BusPartner;
use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\CommissionRecode;
use app\common\model\MachineCate;
use app\common\model\MachineMachine;
use app\common\model\MallCommissionRecode;
use think\App;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;

/**
 * @ControllerAnnotation(title="代理商")
 */
class Cate extends AdminController
{

    use Curd;

    protected array $withAttr = [];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MachineCate();
        $this->assign('getStatusList', $this->model->getStatusList());
        $this->field = "*,id as commission";
    }

    /**
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            if (!empty($this->request->cate_machine)) {

                $where[] = [
                    ['id', '=', $this->request->cate_machine]
                ];
            }
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->field($this->field)
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation (title="获取正常的分组")
     * @return array|\think\response\Json
     */
    public function getCateByList()
    {
        //查询所有正常的分组
        $where = [
            ['status', '=', 1]
        ];
        try {
            $lists = $this->model->getBaseByListAll($where, 'title as name,id as value');
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        return Show::json_show(Status::status_normal, "ok", $lists);
    }

    /**
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @NodeAnotation(title="设置提成比例")
     */
    public function partner($id)
    {
        $row = $this->model->getBaseById($id);
        if ($this->request->isPost()) {
            $partner = $this->request->param('partner', 0, 'float');
            $res = $this->model->updateDataSave($row['id'], [
                'partner' => $partner,
                'update_time' => time()
            ]);
            if ($res) {
                $this->success("成功");
            } else {
                $this->error("失败");
            }
        }
        return $this->fetch('', [
            'row' => $row
        ]);
    }

    /**
     * @param $id
     * @return mixed
     * @NodeAnotation(title="提现")
     * @throws \think\Exception
     */
    public function withdrawal($id)
    {
        $row = $this->model->getBaseById($id);
        if ($this->request->isPost()) {
            $partner = $this->request->param('partner', 0, 'float');
            if ($partner <= 0) {
                $this->error("请输入正确的提现金额");
            }
            //查询提现金额
            $money = (new BusPartner())->getPartnerBySurplus($row['id']);
            if ($money < $partner) {
                $this->error("可提现金额不足");
            }
            $insert_data = [
                "cate_id" => $row['id'],
                "commission" => $partner,

            ];
            try {
                $res = (new MallCommissionRecode())->save($insert_data);
            }catch (\Exception $e){
                $this->error("内部错误".$e->getMessage());
            }
            if($res){
                $this->success("成功");
            }else{
                $this->error("失败");
            }
        }
        return $this->fetch('', [
            'row' => $row
        ]);
    }

}